﻿using CSSetDesktopWallpaper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Windows.Forms;

namespace KonachanChanger.Data
{
    [DataContract]
    class UserPrefs : IExtensibleDataObject
    {
        public static string Path { get { return System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Kamahl", "Konachan", "Prefs.json"); } }

        private static UserPrefs _Instance = null;
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2002:DoNotLockOnObjectsWithWeakIdentity")]
        public static UserPrefs GetInstance()
        {
            lock (typeof(UserPrefs))
            {
                if (_Instance == null)
                    _Instance = Serialization.TryReadObject<UserPrefs>(Path) ?? new UserPrefs();
                return _Instance;
            }
        }
        public UserPrefs()
        {
            DoFirstRun(new StreamingContext());
        }
        [DataMember]
        public CheckState SafeSearch { get; set; }
        [DataMember]
        public int MinutesSliderValue { get; set; }
        [DataMember]
        public WallpaperStyle WallpaperStyle { get; set; }
        private string lastSearch;
        [DataMember]
        public string LastSearch
        {
            get
            {
                return lastSearch;
            }
            set
            {
                lastSearch = value;
                if (lastSearch.Contains("rating:")){
                    var tokens = lastSearch.Split(' ');
                    var rating = tokens.First(t => t.Contains("rating:"));
                    switch (rating)
                    {
                        case "rating:s":
                            SafeSearch = CheckState.Checked;
                            lastSearch = lastSearch.Replace(rating, "");
                            break;
                        case " -rating:e":
                            SafeSearch = CheckState.Indeterminate;
                            lastSearch = lastSearch.Replace(rating, "");
                            break;
                        default:
                            break;
                    }
                }
            }
        }

        [DataMember]
        private bool DoneFirstRun;

        [OnDeserialized]
        private void DoFirstRun(StreamingContext c)
        {
            if (String.IsNullOrWhiteSpace(SavePathS))
                SavePathS = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Kamahl", "Konachan");
            if (String.IsNullOrWhiteSpace(SavePathQ))
                SavePathQ = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Kamahl", "Konachan");
            if (String.IsNullOrWhiteSpace(SavePathE))
                SavePathE = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Kamahl", "Konachan");
            if (DoneFirstRun)
                return;
            SafeSearch = CheckState.Indeterminate;
            WallpaperStyle = CSSetDesktopWallpaper.WallpaperStyle.Fit;
            BackgroundCheckTagMes = true;
            BackgroundGetAllWalls = false;
            BackgroundSort = true;
            BackgroundGetThumbs = true;

            DoneFirstRun = true;
        }
        ~UserPrefs()
        {
            Directory.CreateDirectory(System.IO.Path.GetDirectoryName(Path));
            Kamahl.Common.Serialization.WriteObject(Path, this);
        }

        public ExtensionDataObject ExtensionData { get; set; }

        [DataMember, Category("Background Work")]
        public bool BackgroundGetThumbs { get; set; }
        [DataMember, Category("Background Work")]
        public bool BackgroundSort { get; set; }
        [DataMember, Category("Background Work")]
        public bool BackgroundCheckTagMes { get; set; }
        [DataMember, Category("Background Work")]
        public bool BackgroundGetAllWalls { get; set; }
        [DataMember]
        public string SavePathS
        {
            get
            {
                return _savePathS;
            }
            set
            {
                _savePathS = value;

            }
        }
        private string _savePathS;
        [DataMember]
        public string SavePathQ
        {
            get
            {
                return _savePathQ;
            }
            set
            {
                _savePathQ = value;

            }
        }
        private string _savePathQ;
        [DataMember]
        public string SavePathE
        {
            get
            {
                return _savePathE;
            }
            set
            {
                _savePathE = value;

            }
        }
        private string _savePathE;

        [DataMember, Category("Extensions")]
        public bool EnableP2P { get; set; }
    }
}
